package com.ruoyi.web.controller.system;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mysql.cj.xdevapi.JsonParser;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.FxyFinancialAccountSets;
import com.ruoyi.system.domain.FxyFinancialVoucherTemplateDetails;
import com.ruoyi.system.domain.reqvo.Params;
import com.ruoyi.system.domain.reqvo.VoucherReqVO;
import com.ruoyi.system.service.impl.FxyFinancialAccountSetsServiceImpl;
import com.ruoyi.system.service.impl.FxyFinancialVoucherTemplateDetailsServiceImpl;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.FxyFinancialVoucher;
import com.ruoyi.system.service.IFxyFinancialVoucherService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import springfox.documentation.spring.web.json.Json;

/**
 * 凭证Controller
 *
 * @author ruoyi
 * @date 2025-01-13
 */
@RestController
@RequestMapping("/system/voucher")
public class FxyFinancialVoucherController extends BaseController {
    @Autowired
    private IFxyFinancialVoucherService fxyFinancialVoucherServices;
    @Autowired
    FxyFinancialAccountSetsServiceImpl fxyFinancialAccountSetsService;
    @Autowired
    SysUserServiceImpl sysUserService;

    @Autowired
    private FxyFinancialVoucherTemplateDetailsServiceImpl fxyFinancialVoucherTemplateDetailsService;

    /**
     * 查询凭证列表
     */

    @PostMapping("/list")
    public TableDataInfo list(@RequestBody Params params) throws JsonProcessingException {
        String queryData = params.getQueryData();

        String[] split = queryData.split(",");

        String year = split[0];
        String month = split[1];

        params.setYear(year);
        params.setMonth(month);


        List<FxyFinancialVoucher> list = fxyFinancialVoucherServices.selectFxyFinancialVoucherList(params);

        ObjectMapper objectMapper = new ObjectMapper();
        for (FxyFinancialVoucher financialVoucher : list) {
            String billList = financialVoucher.getBillList();
            List<Object> details = objectMapper.readValue(billList, List.class);
            financialVoucher.setDetails(details);
        }

        return getDataTable(list);
    }

    /**
     * 导出凭证列表
     */

    @Log(title = "凭证", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FxyFinancialVoucher fxyFinancialVoucher) {
        List<FxyFinancialVoucher> list = fxyFinancialVoucherServices.selectFxyFinancialVoucherList(null);
        ExcelUtil<FxyFinancialVoucher> util = new ExcelUtil<FxyFinancialVoucher>(FxyFinancialVoucher.class);
        util.exportExcel(response, list, "凭证数据");
    }

    /**
     * 获取凭证详细信息
     */

    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return success(fxyFinancialVoucherServices.selectFxyFinancialVoucherById(id));
    }

    /**
     * 新增凭证
     */

    @Log(title = "凭证", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody VoucherReqVO voucherReqVO) throws JsonProcessingException {
        AjaxResult ajaxResult = fxyFinancialVoucherServices.insertVoucherList(voucherReqVO);
        return ajaxResult;
    }

    /**
     * 修改凭证
     */

    @Log(title = "凭证", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FxyFinancialVoucher fxyFinancialVoucher) {
        return toAjax(fxyFinancialVoucherServices.updateFxyFinancialVoucher(fxyFinancialVoucher));
    }

    /**
     * 删除凭证
     */

    @Log(title = "凭证", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(fxyFinancialVoucherServices.deleteFxyFinancialVoucherByIds(ids));
    }


    /**
     * @param ids:
     * @Description:审核
     * @Author: xuzhaobo
     * @Date: 2025/1/16 19:27
     * @return: com.ruoyi.common.core.domain.AjaxResult
     */
    @Log(title = "凭证", businessType = BusinessType.UPDATE)
    @GetMapping("/examine/{ids}")
    public AjaxResult examine(@PathVariable Integer[] ids) {
        if (ids == null || ids.length == 0) {
            return AjaxResult.error("请选择审核对象");
        }

        LoginUser loginUser = SecurityUtils.getLoginUser();

        Params params = new Params();
        params.setIds(ids);
        params.setAuditMemberId(loginUser.getUserId());
        params.setAuditMemberName(loginUser.getUsername());

        int examine = fxyFinancialVoucherServices.examine(params);


        if (examine > 0) {
            return AjaxResult.success();
        }

        return AjaxResult.error();
    }

    /**
     * @param ids:
     * @Description:反审核
     * @Author: xuzhaobo
     * @Date: 2025/1/16 19:27
     * @return: com.ruoyi.common.core.domain.AjaxResult
     */
    @Log(title = "凭证", businessType = BusinessType.UPDATE)
    @GetMapping("/ReExamine/{ids}")
    public AjaxResult ReExamine(@PathVariable Integer[] ids) {
        if (ids == null || ids.length == 0) {
            return AjaxResult.error("请先选择");
        }
        int examine = fxyFinancialVoucherServices.ReExamine(ids);
        if (examine > 0) {
            return AjaxResult.success();
        }

        return AjaxResult.error();


    }

    /**
     * @param ids:
     * @Description:作废
     * @Author: xuzhaobo
     * @Date: 2025/1/16 19:27
     * @return: com.ruoyi.common.core.domain.AjaxResult
     */
    @Log(title = "凭证", businessType = BusinessType.UPDATE)
    @GetMapping("/cancellation/{ids}")
    public AjaxResult Cancellation(@PathVariable Integer[] ids) {
        if (ids == null || ids.length == 0) {
            return AjaxResult.error("请先选择");
        }
        int examine = fxyFinancialVoucherServices.Cancellation(ids);
        if (examine > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error();

    }

    /**
     * @param ids:
     * @Description:恢复
     * @Author: xuzhaobo
     * @Date: 2025/1/16 19:26
     * @return: com.ruoyi.common.core.domain.AjaxResult
     */
    @Log(title = "凭证", businessType = BusinessType.UPDATE)
    @GetMapping("/renew/{ids}")
    public AjaxResult renew(@PathVariable Integer[] ids) {
        if (ids == null || ids.length == 0) {
            return AjaxResult.error("请先选择");
        }
        int examine = fxyFinancialVoucherServices.renew(ids);
        if (examine > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error();

    }

    /**
     * @param params:
     * @Description:红冲
     * @Author: xuzhaobo
     * @Date: 2025/1/16 19:26
     * @return: com.ruoyi.common.core.domain.AjaxResult
     */
    @Log(title = "凭证", businessType = BusinessType.UPDATE)
    @PostMapping("/punch")
    public AjaxResult punch(@RequestBody Params params) throws JsonProcessingException {

        return fxyFinancialVoucherServices.selectVoucherById(params);

    }


}





